﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text;
using YiDingCanWp7.Models;
using Newtonsoft.Json;

namespace YiDingCanWp7
{
    public partial class AddShopSellerCat : PhoneApplicationPage
    {
        string shopid = string.Empty;
        List<SimpleSellCat> cats = new List<SimpleSellCat>();

        public AddShopSellerCat()
        {
            InitializeComponent();
    
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            shopid = NavigationContext.QueryString["shopid"];
            if (cats.Count == 0 && !string.IsNullOrEmpty(shopid))
            {
                WebClient webClient = new WebClient();
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers["Accept-type"] = "application/json";
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
                webClient.DownloadStringAsync(new Uri(string.Format(BaseConfig.ShopSellerCatsUrl, shopid)));
            }
           
        }

        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            cats = JsonConvert.DeserializeObject<List<SimpleSellCat>>(e.Result);
            cats.Insert(0, new SimpleSellCat() { Name="根目录",CatId=0});
            listBox_cat.ItemsSource = cats;
            listBox_cat.SelectedIndex = 0;
        }
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            #region 检验提交的数据是否正确

            if (string.IsNullOrEmpty(NameTextBox.Text.Trim()))
            {
                MessageBox.Show("请填写分类名称！");
                return;
            }
            #endregion
            performanceProgressBar.IsIndeterminate = true;
            var webClient = new WebClient();
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
            SellerCat cat = new SellerCat() { 
                Name=NameTextBox.Text.Trim(),
                DeviceUniqueId=PhoneUtils.GetDeviceUniqueId(),
                Parent_SellerCatId=((SimpleSellCat)listBox_cat.SelectedItem).CatId
            };
            var json = JsonConvert.SerializeObject(cat);
            webClient.UploadStringAsync(new Uri(BaseConfig.BaseUrl + "/Shop/AddShopReview"), "POST", json);
        }
        void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
              
                MessageBox.Show(e.Result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                performanceProgressBar.IsIndeterminate = false;
            }
        }
    }
    public class SimpleSellCat {
        public int CatId { get; set; }
        public string Name { get; set; }
    }
}